//
//  SelectWorkViewController.m
//  fitnessmanage
//
//  Created by wujian on 2017/12/14.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  选择教练和销售

#import "SelectWorkViewController.h"
#import "SelectWorkCollectionViewCell.h"
//#import "SelectPrivateClassViewController.h"

@interface SelectWorkViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    
    int startIndex;
    
    int pageSize;
}

@property(nonatomic,weak)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,assign)NSInteger selectRow;

@end

@implementation SelectWorkViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.selectRow = -1;
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateNormal];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [self createSubView];
    
    if ([self.from isEqualToString:@"教练"]) {
        
        self.title = @"选择教练";
    }
    else if([self.from isEqualToString:@"销售顾问"]){
        
        self.title = @"选择销售";
    }
    else if ([self.from isEmptyString]){
        
        self.title = @"选择介绍人";
    }
}

#pragma mark 下拉
-(void)loadStartData{
    
    startIndex = 1;
    
    pageSize = 20;
    
    [self loadData];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    startIndex = startIndex + 1;
    
    [self loadData];
}

#pragma mark 加载数据
-(void)loadData{
    
    //加载数据
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    VenueModel *venueModel = [VenueModel venue];
    
    [params setObject:venueModel.ID forKey:@"ShopID"];
    
    [params setObject:self.from forKey:@"TypeName"];
    
    if ([self.from isEqualToString:@"销售顾问"]) {
     
        [params setObject:@"" forKey:@"TypeName"];
    }
    
    [params setObject:[NSString stringWithFormat:@"%d",startIndex] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",pageSize] forKey:@"rows"];
    
    [HTTPTool GET:Get_UsersListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count > 0) {
                      
                      [self.collectionView.mj_header endRefreshing];
                      
                      [self.collectionView.mj_footer endRefreshing];
                  }
                  else{
                      
                      [self.collectionView.mj_header endRefreshing];
                      
                      [self.collectionView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  
                  if (startIndex == 1) {
                      
                      self.dataArray = [NSMutableArray arrayWithArray:resultArray];
                      
                      if (self.dataArray.count == 0) {
                          
                          self.noDataView = [NoDataView shareInstance];
                          
                          [self.noDataView showInView:self.view];
                      }
                  }
                  else{
                      
                      [self.dataArray addObjectsFromArray:resultArray];
                  }
                  
                  //                  self.countLabel.text = [NSString stringWithFormat:@"%ld",self.dataSource.count];
                  
                  [self.collectionView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.collectionView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
    
}

-(void)createSubView{
    
    UICollectionViewFlowLayout *collectViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置cell之间间距
    collectViewFlowLayout.minimumInteritemSpacing = 15;
    
    //设置行距
    collectViewFlowLayout.minimumLineSpacing = 15;
    
    collectViewFlowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    CGFloat itemWith = (SCREEN_WIDTH - 50) / 3;
    
    //设置Item大小
    collectViewFlowLayout.itemSize = CGSizeMake(itemWith, itemWith);
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:collectViewFlowLayout];
    
    collectionView.showsHorizontalScrollIndicator = NO;
    
    collectionView.showsVerticalScrollIndicator = NO;
    
    collectionView.backgroundColor = CSWhiteColor;
    
    collectionView.delegate = self;
    
    collectionView.dataSource = self;
    
    [collectionView registerClass:[SelectWorkCollectionViewCell class] forCellWithReuseIdentifier:@"SelectWorkCollectionViewCell"];
    
    [self.view addSubview:collectionView];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.automaticallyHidden = YES;
    
    
    MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
    collectionView.mj_header = mj_head;
    collectionView.mj_footer = footer;
    [collectionView.mj_header beginRefreshing];
    
    [collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
    }];
    
    self.collectionView = collectionView;
}

#pragma mark CollectionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectWorkCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectWorkCollectionViewCell" forIndexPath:indexPath];
    
    NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
    
    cell.nameLabel.text =[NSString stringWithFormat:@"%@",[dict objectForKey:@"NickName"]];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",[dict objectForKey:@"Pic"],mid_image];
    
    urlStr=[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:nil];
    
    if (self.selectRow == indexPath.row) {
        
        cell.isSelect = YES;
    }
    else{
        
        cell.isSelect = NO;
    }
    
    return cell;
}


#pragma mark UICollectionDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.selectRow = indexPath.row;
    
    [collectionView reloadData];
}


#pragma mark 右侧按钮点击
-(void)rightBtnClick{
    
    if (self.selectRow == -1) {
        
        [MBProgressHUD showText:[NSString stringWithFormat:@"请选择%@",self.from]];
        
        return;
    }
    
    NSDictionary *dict = [self.dataArray objectAtIndex:self.selectRow];
    
    NSString *name =[NSString stringWithFormat:@"%@",[dict objectForKey:@"NickName"]];
    
    NSString *workId = [NSString stringWithFormat:@"%@",[dict objectForKey:@"ID"]];
    
    if (self.sendSelectWorkerBlock) {
        
        self.sendSelectWorkerBlock(workId, name);
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        
        //排私教课的时候选择教练
#warning czq
//        NSString *workId = [NSString stringWithFormat:@"%@",[dict objectForKey:@"ID"]];
//
//        SelectPrivateClassViewController *selectPrivateClassVC = [[SelectPrivateClassViewController alloc] init];
//
//        selectPrivateClassVC.workId = workId;
//
//        [self.navigationController pushViewController:selectPrivateClassVC animated:YES];
    }
}

#pragma mark 数组
-(NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        
        _dataArray = [[NSMutableArray alloc] init];
    }
    
    return _dataArray;
}

@end
